const app = getApp();
const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
Page({
	data: {
		kcList: [],
		latitude: "",
		longitude: "",
		windowWidth: app.systemInfo.windowWidth,
		windowHeight: app.systemInfo.windowHeight,
		pixelRatio: app.systemInfo.pixelRatio,
		addressName: "",//显示当前位置
		courseTime: "",//显示当前日期
		ageRanges:"",//宝宝年龄段
		date: "",//课程时间查询
		coursePointRange:"",//遛点范围
		courseTypeName:"",//课程类型
		tradeArea:"",//课程地址
		ld: "",//显示头部遛点数
		showLd: true,//显示头部遛点提示
		redShow: false,//显示红包弹框
		pageNum:1,//当前页
		total:0,//总条数
	},
	onLoad: function(options) {
		let _this = this;
		_this.setData({
			courseTime: util.formatDate(new Date()),
			date: util.formatTime(new Date(), 0)
		});
		//获取小程序用户信息，并保存
		app.getUserInfo(function(item) {
			api.saveUserInfo({
				data: {
					appid: "wxef0256a5461356df",
					secret: "6dd00f32c68c4a245bad74ecc1dece29",
					jsCode: item.code,
					encryptedData: item.encryptedData,
					iv: item.iv
				},
				success: function(res) {
					app.globalData.userId = res.data.userId;
					if(app.globalData.latitude == "") {
						app.getLocation(function(latitude, longitude) {
							_this.getSiteName(longitude, latitude);
						});
					}
				}
			});
		});
	},
	onShow(){
		//查询遛点
		this.findLd();
		if(app.globalData.userId != ""){
			this.getParents();

			//查询条件
			if(app.search != null){
				let age = app.search.age;
				let liudian = app.search.liudian;
				let kctype = app.search.kctype;
				let address = app.search.address;
				if(age != undefined){
					this.setData({
						ageRanges: age
					});
				}
				if(liudian != undefined){
					this.setData({
						coursePointRange: liudian
					});
				}
				if(kctype != undefined){
					this.setData({
						courseTypeName: kctype
					});
				}
				if(address != undefined){
					this.setData({
						tradeArea: address
					});
				}
				if(address != undefined || kctype != undefined || liudian != undefined || age != undefined){
					this.searchList();
				}
			}
		}
	},
	//定位当前位置
	getSiteName(lng, lat) {
		let _this = this;
		api.getSiteName({
			data: {
				userId: app.globalData.userId,
				lng: lng,
				lat: lat
			},
			success: function(res) {
				let adJson = JSON.parse(res.data.msg);
				_this.setData({
					addressName: adJson.result.formatted_address,
					latitude: lat,
					longitude: lng
				});
				_this.searchList();
			}
		});
	},
	getParents(){
		let _this = this;
		api.getParents({
			data: {
				id:app.globalData.userId
			},
			success: function(res) {
				if(res.data.obj.phone == null){
					//显示红包窗口
					_this.setData({
						redShow: true,
					})
				}else{
					//显示红包窗口
					_this.setData({
						redShow: false,
					})
				}
			}
		});
	},
	//关闭遛点提示
	closeTep() {
		this.setData({
			showLd: false
		});
	},
	//根据条件查询活动列表
	searchList() {
		let _this = this;
		let data = {
			pageSize: 20,
			pageNum: _this.data.pageNum,
			lng: _this.data.longitude,
			lat: _this.data.latitude,
			courseTime:_this.data.date,
		};
		if(_this.data.coursePointRange != ""){
			data.coursePointRange = _this.data.coursePointRange
		}
		if(_this.data.tradeArea != ""){
			data.tradeArea = _this.data.tradeArea
		}
		if(_this.data.courseTypeName != ""){
			data.courseTypeName = _this.data.courseTypeName
		}
		if(_this.data.ageRanges != ""){
			data.ageRanges = _this.data.ageRanges
		}
		api.wxlist({
			data: data,
			success: function(res) {
				res.data.list.rows.forEach(function(item) {
					if(item.courseImgs != null && item.courseImgs != undefined) {
						item.courseImgs = item.courseImgs.split(",")[0];
					}
					if(item.enterStartTime != null){
						item.enterStartTime = util.formatEndTime(item.enterStartTime);
					}else{
						if(item.subTime != null) {
							item.enterStartTime = util.formatEndTime(item.subTime);
						}
					}
					if(item.subTime != null) {
						item.subTime = util.formatHour(new Date(item.subTime));
					}
				});
				_this.setData({
					kcList: res.data.list.rows,
					total:res.data.list.total
				});
			}
		});
	},
	//翻页查询
	nextPage(){
		let _this = this;
		if(this.data.total > this.data.pageNum * 20){
			setTimeout(function(){
				_this.setData({
					pageNum: this.data.pageNum + 1
				});
				_this.searchList();
			},500);
		}else{
			console.log(this.data.pageNum)
		}
	},
	onPullDownRefresh:function(){
		this.searchList();
		wx.stopPullDownRefresh();
	},
	//查询遛点配置
	findLd() {
		let _this = this;
		api.getMateList({
			data: {
				pageSize: "10",
				pageNum: "1",
				type: "liudian"
			},
			success: function(res) {
				_this.setData({
					ld: res.data.list.rows[0].value
				});
			}
		});
	},
	//根据时间搜索
	bindDateChange: function(e) {
		this.setData({
			courseTime: util.formatDate(new Date(e.detail.value)),
			date: util.formatTime(new Date(e.detail.value), 0)
		});
		this.searchList();
	},
	//前一天搜索
	yesterdayDate: function() {
		this.setData({
			courseTime: util.yesterdayDate(new Date(this.data.date)),
			date: util.formatTime(new Date(this.data.date), -1)
		});
		this.searchList();
	},
	//后一天搜索
	tomorrowDate: function() {
		this.setData({
			courseTime: util.tomorrowDate(new Date(this.data.date)),
			date: util.formatTime(new Date(this.data.date), 1)
		});
		this.searchList();
	},
	//跳转到搜索页面
	toSearch: function() {
		wx.navigateTo({
			url: '../search/search'
		})
	},
	//跳转地址搜索页面
	toAddressSearch: function() {
		wx.navigateTo({
			url: '../address/search?adJson=' + this.data.addressName
		})
	},
	//跳转详情页面
	toDetail: function(event) {
		let id = event.currentTarget.dataset.id;
		let subId = event.currentTarget.dataset.subId;
		wx.navigateTo({
			url: '../kcdetail/kcdetail?id=' + id + "&subId=" + subId
		})
	},
	//关闭红包弹框
	closeRed: function() {
		this.setData({
			redShow: false
		});
	},
	//跳转到红包界面
	openRed: function() {
		wx.navigateTo({
			url: '../fetchred/red?isNew='+this.data.redShow
		})
	}
});